﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MS_Entity;
using DAL;

namespace MS_UI
{
    public partial class FrmLogin : Form
    {
        public FrmLogin()
        {
            InitializeComponent();
            label1.Parent = pic_main;
            label2.Parent = pic_main;
            chk_UsePwd.Parent = pic_main;
        }

        private void btn_Login_Click(object sender, EventArgs e)
        {
            // 判断是否选中匿名登录
            if (this.chk_UsePwd.Checked)
            {
                this.DialogResult = DialogResult.OK;
                CommonMethod.objAdmins.LoginName = "None";
            }
            else
            {
                // 登录判断
                if (this.txt_User.Text.Trim().Length == 0)
                {
                    MessageBox.Show("请填写用户名！","登录提示");
                    this.txt_User.Focus();
                    return;
                }
                if (!DataValidate.IsInteger(this.txt_User.Text.Trim()))
                {
                    MessageBox.Show("用户名必须为正整数！", "登录提示");
                    this.txt_User.Focus();
                    return;
                }
                if (this.txt_User.Text.Trim().Length == 0)
                {
                    MessageBox.Show("请输入密码！", "登录提示");
                    this.txt_User.Focus();
                    return;
                }
                // 封装对象
                SysAdmins objAdmins = new SysAdmins()
                {
                    LoginId = Convert.ToInt32(this.txt_User.Text.Trim()),
                    LoginPwd = this.txt_Pwd.Text.Trim(),
                };
                if (AdminLogin(objAdmins) == null)
                {
                    MessageBox.Show("用户名或密码错误！", "登录提示");
                }
                else
                {
                    this.DialogResult = DialogResult.OK;
                }

            }
        }

        // 登录判断方法
        private SysAdmins AdminLogin(SysAdmins objAdmin)
        {
            string sql = "Select LoginName,Role from sysAdmins where LongId={0} and LoginPwd = {1}";
            sql = string.Format(sql, objAdmin.LoginId, objAdmin.LoginPwd);
            DataSet ds = SQLHelper.GetDataSet(sql);
            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                objAdmin.LoginName = ds.Tables[0].Rows[0]["LoginName"].ToString();
                objAdmin.Role = Convert.ToInt32(ds.Tables[0].Rows[0]["Role"]);
                return objAdmin;
            }
            else
            {
                return null;
            }
        }

        private void txt_User_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                this.btn_Login_Click(null,null);
            }
        }

        private void txt_Pwd_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                this.btn_Login_Click(null, null);
            }
        }

        private void btn_Exit_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
